package org.hagopa.games.moneypool.game;

import java.util.List;

import org.hagopa.games.moneypool.dblayer.GameGeneralInfoAdopt;
import org.hagopa.games.moneypool.dblayer.Connector.DBConnector;
import org.hagopa.games.moneypool.game.Bank;
import org.hagopa.games.moneypool.game.gameobjects.Board;
import org.hagopa.games.moneypool.game.gameobjects.Field;
import org.hagopa.games.moneypool.game.gameobjects.PropertyStatusEnum;
import org.hagopa.games.moneypool.msgs.CommandsEnum;
import org.hagopa.games.moneypool.msgs.Message;
import org.hagopa.games.moneypool.msgs.ContentTypes.game.DiesThrow;
import org.hagopa.games.moneypool.msgs.ContentTypes.game.UpdateStatus;
import org.hagopa.games.moneypool.msgs.ContentTypes.service.EmptyContent;
import org.hagopa.games.moneypool.network.NetClientHandler;

public class GameProcedure {
	
	//This class should be responsible for update Game status from PlayerHolder actions
	// and return status to player for further transfer to client
		
	private GameGeneralInfoAdopt gameData = null;
	private Bank gameBank = null;
	private Board locBoard = null;
	private DiesPair localDies = new DiesPair();
	private boolean started = false; 
	private static int activePlayer = 0;
	private UpdateStatus updStatusList = null; //
	
	public GameProcedure(GameGeneralInfoAdopt gd)
	{
		this.gameData = gd;
	}
	public void init()
	{
		
		//this.locBoard = new Board();
		DBConnector.connect();

	}
	public void startGame()
	{
		System.out.println("Starting Game");
		this.started = true;
	}
	
	static public int getSctivePlayer()
	{
		return activePlayer;
	}
	
	public  List<UpdateStatus> setupUpdateList()
	{
		List<UpdateStatus> plList = null;
		for(int i = 0; i< gameData.getMax_players(); i++)
		{
			//TODO what?
		}
		return plList;
	}
	public void getPlayerAction(PlayerHolder ph)
	{
		System.out.println("Inside getPlayerAction");
		//this.locBoard.printStatus();
		
		if(null == ph)
			return;
		
		if(ph.getGId() > 6 || ph.getGId() < 1)
			return;
		
		UpdateStatus ploc = null;
		
		Message msg = ((NetClientHandler)ph.getClientHandler()).getLastClientMsg(); 
		
		Message msg4Client = new Message();
		msg4Client.setClientId(msg.getClientId());
		msg4Client.setSessionId(msg.getSessionId());
		
		System.out.println("Command: " +msg.getCommand().getName());
		Field currField = null;
		switch(msg.getCommand())
		{
			case AC_NONE:
				break;
				
			case AC_BUY:
				currField = this.locBoard.getFieldById(ph.getCurrPosition());
				if(currField.getPropOwner() != 0 && currField.getPropOwner() != ph.getGId())
				{	
					System.out.println("Player tries to buy property he is not allowed to buy");
					EmptyContent contentHandler = new EmptyContent();
					
					msg4Client.setCommand(CommandsEnum.DENY);
					msg4Client.setContentSz(0);
					msg4Client.setContentHandler(contentHandler);
					return;
				}
				else if(currField.getPropStatus() == PropertyStatusEnum.MORGAGED)
				{
					System.out.println("Cannot buy this property!!! It is mortgaged");
					EmptyContent contentHandler = new EmptyContent();
					
					msg4Client.setCommand(CommandsEnum.DENY);
					msg4Client.setContentSz(0);
					msg4Client.setContentHandler(contentHandler);
					return;
				}
				else
				{
					if(ph.getCash() < currField.getProp().getCurrCost())
					{
						System.out.println("Player does not have enough money");
						EmptyContent contentHandler = new EmptyContent();
						
						msg4Client.setCommand(CommandsEnum.DENY);
						msg4Client.setContentSz(0);
						msg4Client.setContentHandler(contentHandler);
						return;
					}
					currField.setPropOwner(ph.getGId());
					ph.setCash(ph.getCash() - currField.getProp().getCurrCost());
				}
				break;
				
			case AC_SELL:
				currField = this.locBoard.getFieldById(ph.getCurrPosition());
				if(currField.getPropOwner() != ph.getGId())
				{	
					System.out.println("Player tries to sell property he is not an owner");
					EmptyContent contentHandler = new EmptyContent();
					
					msg4Client.setCommand(CommandsEnum.DENY);
					msg4Client.setContentSz(0);
					msg4Client.setContentHandler(contentHandler);
					return;
				}
				else
				{
					
				}
				
				break;
			case AC_PAY:
				
				break;
			case AC_MORTGAGE:
				currField = this.locBoard.getFieldById(ph.getCurrPosition());
				ploc = (UpdateStatus)msg.getContentHandler();
				if(/*ploc.getOwner() !=*/ ph.getGId() == 0)
				{
					System.out.println("Player Tries to Mortgage not his property");
					EmptyContent contentHandler = new EmptyContent();
					
					msg4Client.setCommand(CommandsEnum.DENY);
					msg4Client.setContentSz(0);
					msg4Client.setContentHandler(contentHandler);
					
					return;
					
				}
				else
				{
					currField.setPropStatus(PropertyStatusEnum.MORGAGED);
					
					this.gameBank.decCash(currField.getProp().getMortgageCost());
					
					ph.setCash(ph.getCash() + currField.getProp().getMortgageCost());
					//i should send to client two structures????? PlayerLocation + PropertyStatus?
				}
				((NetClientHandler)ph.getClientHandler()).setMsg4Client(msg4Client);
				break;
			case AC_UPGRADE:
				currField = this.locBoard.getFieldById(ph.getCurrPosition());
				//((PropertyStatus)msg.getContentHandler()).getUpgradeLevel();
				
				ploc = new UpdateStatus();

				msg4Client.setCommand(CommandsEnum.UPDATE);
				msg4Client.setContentSz(28);
				msg4Client.setContentHandler(ploc);
				((NetClientHandler)ph.getClientHandler()).setMsg4Client(msg4Client);
				break;
			case THR_DIES:
				
				msg4Client.setCommand(CommandsEnum.UPDATE);
				
				DiesThrow dthr = new DiesThrow();
				this.localDies.generateNewThrow();
				
				dthr.setFirst(this.localDies.getFirstD());
				dthr.setSecond(this.localDies.getSeconfD());
				
				ph.setCurrPosition(ph.getCurrPosition() + (this.localDies.getFirstD()+ this.localDies.getSeconfD()), 0);
				
				msg4Client.setContentSz(16);
				msg4Client.setContentHandler(dthr);
				
				((NetClientHandler)ph.getClientHandler()).setMsg4Client(msg4Client);
				
				break;
			case MOVE_DONE:
				msg4Client.setCommand(CommandsEnum.UPDATE);
				
				UpdateStatus pl = new UpdateStatus();

				
				msg4Client.setContentHandler(pl);
				msg4Client.setContentSz(20);
				((NetClientHandler)ph.getClientHandler()).setMsg4Client(msg4Client);
				
				break;
			case END_TURN:
				System.out.println("End of Turn! Player Id:" + ph.getGId());
				ph.setTurnStatus(0);
				activePlayer = (ph.getGId()== this.gameData.getMax_players())?1:ph.getGId() + 1;
				msg4Client.setCommand(CommandsEnum.END_TURN);
				UpdateStatus pl1 = new UpdateStatus();

				msg4Client.setContentHandler(pl1);
				msg4Client.setContentSz(20);
				((NetClientHandler)ph.getClientHandler()).setMsg4Client(msg4Client);
				break;
			default:
					break;
			
		}
	}
	public boolean isStarted() {
		return started;
	}
	
	
	
	 
}
